import React, {PureComponent} from "react";
import {connect} from "react-redux";
import {Form} from "antd";
import {query} from "./actions";
import moment from "moment";
import {DATE_FORMAT} from "../../lib/func";
import FilterForm from "../../components/FilterForm";
import {getCurrentOrganization, getLastFilter, getPrincipal, saveLastFilter} from "../../lib/identity";
import {getCarrierList, getClientList} from "../home/actions";
const LAST_FILTER = "ghOrder";

class Filter extends PureComponent {
    state = {};
    handleSearch = (values) => {
        const {dispatch, pageSize} = this.props;
        let fields = values;
        this.setState({...fields});
        if (fields.actDepartureTimeRange && fields.actDepartureTimeRange.length > 1) {
            fields.actDepartureTimeStart = fields.actDepartureTimeRange[0].format(DATE_FORMAT);
            fields.actDepartureTimeEnd = fields.actDepartureTimeRange[1].format(DATE_FORMAT);
        }
        if (fields.createTimeRange && fields.createTimeRange.length > 1) {
            fields.createTimeStart = fields.createTimeRange[0].format(DATE_FORMAT);
            fields.createTimeEnd = fields.createTimeRange[1].format(DATE_FORMAT);
        }
        if (fields.receiveDateRange && fields.receiveDateRange.length > 1) {
            fields.receiveDateStart = fields.receiveDateRange[0].format(DATE_FORMAT);
            fields.receiveDateEnd = fields.receiveDateRange[1].format(DATE_FORMAT);
        }
        if (fields.pickDateRange && fields.pickDateRange.length > 1) {
            fields.pickDateStart = fields.pickDateRange[0].format(DATE_FORMAT);
            fields.pickDateEnd = fields.pickDateRange[1].format(DATE_FORMAT);
        }
        fields.userId = getPrincipal().id;
        delete fields.actDepartureTimeRange;
        delete fields.createTimeRange;
        delete fields.receiveDateRange;
        delete fields.pickDateRange;
        saveLastFilter(LAST_FILTER, {...fields});
        dispatch(query({
            ...fields,
            "userId": getPrincipal().id,
            "organizationId": getCurrentOrganization(),
            page: 1,
            pageSize
        }));
    };

    changeOrg = value => {
        const {dispatch} = this.props;
        this.setState({"organizationId": value});
        dispatch(getClientList(value));
        dispatch(getCarrierList(value));
    };

    render() {
        const {loading, clientList, carrierList, organizations} = this.props;
        let fields = getLastFilter(LAST_FILTER);
        if (fields === null || fields === undefined) {
            fields = {};
        }
        const filterSchema = [
            {
                key: 'orderNumber',
                field: 'orderNumber',
                type: 'text',
                expandable: true,
                width: 200,
                title: '订单编号',
                fieldOptions: {
                    initialValue: this.state.orderNumber || fields.orderNumber
                }
            }, {
                key: 'bookNumber',
                field: 'bookNumber',
                type: 'text',
                expandable: true,
                width: 200,
                title: '订舱号',
                fieldOptions: {
                    initialValue: this.state.bookNumber || fields.bookNumber
                }
            }, {
                key: 'clientId',
                field: 'clientId',
                type: 'listSelector',
                expandable: true,
                title: '客户',
                fieldOptions: {
                    initialValue: this.state.clientId || fields.clientId
                },
                controlProps: {
                    dataSource: clientList,
                    labelField: "name",
                    valueField: "id"
                }
            }, {
                key: 'carrierId',
                field: 'carrierId',
                type: 'listSelector',
                expandable: true,
                width: 200,
                title: '供应商',
                fieldOptions: {
                    initialValue: this.state.carrierId || fields.carrierId
                },
                controlProps: {
                    dataSource: carrierList,
                    labelField: "name",
                    valueField: "id"
                }
            }, {
                key: 'actDepartureTimeRange',
                field: 'actDepartureTimeRange',
                type: 'dateRangePicker',
                expandable: true,
                title: '发车日期',
                fieldOptions: {
                    initialValue: this.state.actDepartureTimeRange || fields.actDepartureTimeRange
                }
            }, {
                key: 'pickDateRange',
                field: 'pickDateRange',
                type: 'dateRangePicker',
                expandable: true,
                title: '提货日期',
                fieldOptions: {
                    initialValue: this.state.pickDateRange || fields.pickDateRange
                }
            }, {
                key: 'receiveDateRange',
                field: 'receiveDateRange',
                type: 'dateRangePicker',
                expandable: true,
                title: '派送日期',
                fieldOptions: {
                    initialValue: this.state.receiveDateRange || fields.receiveDateRange
                }
            }
            , {
                key: 'createTimeRange',
                field: 'createTimeRange',
                type: 'dateRangePicker',
                expandable: true,
                title: '创建日期',
                fieldOptions: {
                    initialValue: this.state.createTimeRange || fields.createTimeRange
                }
            }
        ];

        return (
            <FilterForm schema={filterSchema} callback={this.handleSearch} loading={loading}/>
        );
    }
}

const mapStateToProps = state => {
    return {
        ...state.ghOrder.list,
        organizations: state.home.organizations,
        clientList: state.home.clients,
        carrierList: state.home.carriers
    };
};

export default connect(mapStateToProps)(Form.create()(Filter));
